import { Injectable } from '@nestjs/common'
import { CreateJobDto } from './dto/create-job.dto'
import { UpdateJobDto } from './dto/update-job.dto'
import { Repository, Not, IsNull } from 'typeorm'
import { InjectRepository } from '@nestjs/typeorm'
import { Job } from './entities/job.entity'
import { Department } from '../department/entities/department.entity'
import { Account } from '../account/entities/account.entity'
import { formatOptionData } from 'src/utils'
@Injectable()
export class JobService {
  constructor(
    @InjectRepository(Job) private readonly job: Repository<Job>,
    @InjectRepository(Department)
    private readonly department: Repository<Department>,
    @InjectRepository(Account)
    private readonly account: Repository<Account>
  ) {}
  async create(createJobDto: CreateJobDto) {
    const job = new Job()
    const department = await this.department.findOne({
      where: { id: createJobDto.departmentId }
    })
    const account = await this.account.findOne({
      where: { id: createJobDto.accountId }
    })
    job.name = createJobDto.name
    job.desc = createJobDto.desc
    job.account = account
    job.department = department
    return this.job.save(job)
  }

  async findAll(params: {
    page: number
    pageSize: number
    departmentId?: number
    isDelete?: boolean
  }) {
    const where = {}
    params.isDelete && (where['deleteDate'] = Not(IsNull()))
    params.departmentId && (where['department'] = { id: params.departmentId })
    const data = await this.job.find({
      where: { ...where },
      relations: ['department', 'account'],
      withDeleted: params.isDelete,
      skip: (params.page - 1) * params.pageSize,
      take: params.pageSize
    })
    const newData = []
    data.forEach(item => {
      const { department, account, ...rest } = item
      const newItem = {
        ...rest,
        departmentId: department.id,
        accountId: account.id
      }
      newData.push(newItem)
    })
    const total = await this.job.count({
      where: { ...where },
      withDeleted: params.isDelete
    })
    return { data: newData, total }
  }

  async findAllOption(id: number) {
    const data = await this.job.find({
      where: { department: { id } },
      relations: ['department']
    })
    return formatOptionData(data)
  }

  async findSalary(id: number) {
    const data = await this.job.findOne({
      where: { id },
      relations: ['account']
    })
    return data.account.allSalary
  }

  async update(id: number, updateJobDto: UpdateJobDto) {
    const { departmentId, accountId, ...rest } = updateJobDto
    const department = await this.department.findOne({
      where: { id: departmentId }
    })
    const account = await this.account.findOne({ where: { id: accountId } })
    const newData = { ...rest, department, account }
    return this.job.update(id, newData)
  }

  remove(ids: number[]) {
    return this.job.softDelete(ids)
  }

  thoroughRemove(ids: number[]) {
    return this.job.delete(ids)
  }

  restore(ids: number[]) {
    return this.job.restore(ids)
  }
}
